/******************************************************************************
 * Filename : uc_socket.h
 * Created  : 2009-8-8 by franco.yin
 * Description -
 * 
 ******************************************************************************/

#ifndef _UC_SOCKET_H
#define _UC_SOCKET_H

#ifdef __cplusplus
extern "C" {
#endif

#include "uc_public.h"
#include "uc_time.h"
#include "uc_net.h"

#define UCSOCKET_LOCAL	"127.0.0.1"
#define UCSOCKET_ALL		NULL
#define	SA					struct sockaddr
#define	SA_IN				struct sockaddr_in
#define	SA_UN				struct sockaddr_un

#define	UC_SOCKET_RECV_TIMEOUT	14000
#define	UC_SOCKET_SEND_TIMEOUT	14000

#define UC_TCP SOCK_STREAM
#define UC_UDP SOCK_DGRAM

#define	uc_close(fd) close(fd);
#define	ucsock_client_tcp(ip, port)	ucsock_client(ip, port, SOCK_STREAM);
#define	ucsock_client_timeout_tcp(ip, port, timeout)	ucsock_client_timeout(ip, port, SOCK_STREAM, timeout, NULL);
#define	ucsock_client_timeout_tcp_dev(ip, port, timeout, dev)	ucsock_client_timeout(ip, port, SOCK_STREAM, timeout, dev);
#define	ucsock_client_udp(ip, port)	ucsock_client(ip, port, SOCK_DGRAM);
#define	ucsock_client_timeout_udp(ip, port, timeout)	ucsock_client_timeout(ip, port, SOCK_DGRAM, timeout, NULL);
#define	ucsock_client_timeout_udp_dev(ip, port, timeout, dev)	ucsock_client_timeout(ip, port, SOCK_DGRAM, timeout, dev);
#define ucsock_server_unix_tcp(sock_path) ucsock_server_unix(sock_path, SOCK_STREAM);
#define ucsock_client_unix_tcp(sock_path) ucsock_client_unix(sock_path, SOCK_STREAM);
#define ucsock_server_unix_udp(sock_path) ucsock_server_unix(sock_path, SOCK_DGRAM);
#define ucsock_client_unix_udp(sock_path) ucsock_client_unix(sock_path, SOCK_DGRAM);

extern int ucsock_socket(int domain, int type, int protocol);
extern int ucsock_socketpair(int domain, int type, int protocol, int sockvec[2]);
extern int ucsock_bind(int fd, SA_IN *addr);
extern int ucsock_connect(int sockfd, const SA *serv_addr, socklen_t addrlen);
extern int ucsock_accept(int fd);
extern int ucsock_recv(int fd, char *buf, int len);
extern int ucsock_recvn(int fd, char *buf, int len, int timout_ms);
extern int ucsock_send(int fd, char *buf, int len);
extern int ucsock_sendn(int fd, char *buf, int len, int timout_ms);
extern int ucsock_recvfrom(int fd, char *buf, int len, SA_IN *addr, socklen_t *addr_len);
extern int ucsock_recvfrom_nonblock(int fd, char *buf, int len, SA_IN *addr, socklen_t *addr_len, int *block);
extern int ucsock_recv_msg(int fd, ucmsg_s *pMsg, int len, int timout_ms);

extern int ucsock_sendto(int fd, char *buf, int len, SA_IN *addr);
extern int ucsock_sendto_nonblock(int fd, char *buf, int len, SA_IN *addr, int *block);
extern int ucsock_recv_nonblock(int fd, char *buf, int len, int *block);
extern int ucsock_send_nonblock(int fd, char *buf, int len, int *block);

extern int ucsock_connect_nonblock(char *ipstr, int port, int type, char *dev);

extern char *ucsock_inet_ntoa(UC_U32 ipaddr);
extern char *ucsock_inet_addr_ntoa_r(struct in_addr ina, char *buf);
extern char *ucsock_inet_ntoa_r(UC_U32 ipaddr, char *buf);
extern UC_U32 ucsock_inet_aton(char *ipstr);
extern int ucsock_server(char *ipstr, int port);
extern int ucsock_local_server(char *sock_path);
extern int ucsock_server_udp(char *ipstr, int port);
extern int ucsock_server_udp_group(char *group, char *ipstr, int port);
extern int ucsock_client(char *ipstr, int port, int type);
extern int ucsock_local_client(char *sock_path, int type);
extern int ucsock_client_timeout(char *ipstr, int port, int type, int timeout_ms, char *dev);
extern int ucsock_server_unix(char *sun_path, int type);
extern int ucsock_client_unix(char *sun_path, int type);
extern int ucsock_fd_recv_timeout(int fd, int timeout_ms);
extern int ucsock_fd_send_timeout(int fd, int timeout_ms);
extern int ucsock_fd_set_nodelay(int fd);
extern int ucsock_fd_set_sendbuf(int fd, int buf_size);
extern int ucsock_fd_set_recvbuf(int fd, int buf_size);
extern int ucsock_fd_set_linger(int fd, int on_off, int timeout_s);
extern int ucsock_fd_bind_to_device(int fd, char *ifname);
extern int ucsock_fd_get_info(int fd, char *ipstr, int *port);
extern int ucsock_fd_set_nonblock(int fd);
extern int ucsock_fd_set_block(int fd);

#ifdef __cplusplus
}
#endif

#endif				/* _UC_SOCKET_H */
